package com.cinema.dao.impl;

import com.cinema.dao.MovieDao;
import com.cinema.db.DatabaseManager;
import com.cinema.entity.Movie;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 电影数据访问实现类
 */
public class MovieDaoImpl implements MovieDao {
    
    private final DatabaseManager dbManager = DatabaseManager.getInstance();
    
    @Override
    public void addMovie(Movie movie) {
        String sql = "INSERT INTO movies (id, name, duration) VALUES (?, ?, ?)";
        try {
            dbManager.executeUpdate(sql, movie.getId(), movie.getName(), movie.getDuration());
        } catch (SQLException e) {
            throw new RuntimeException("添加电影失败", e);
        }
    }
    
    @Override
    public Movie getMovieById(String id) {
        String sql = "SELECT id, name, duration FROM movies WHERE id = ?";
        try (ResultSet rs = dbManager.executeQuery(sql, id)) {
            if (rs.next()) {
                return mapResultSetToMovie(rs);
            }
            return null;
        } catch (SQLException e) {
            throw new RuntimeException("查询电影失败", e);
        }
    }
    
    @Override
    public List<Movie> getAllMovies() {
        String sql = "SELECT id, name, duration FROM movies ORDER BY id";
        List<Movie> movies = new ArrayList<>();
        try (ResultSet rs = dbManager.executeQuery(sql)) {
            while (rs.next()) {
                movies.add(mapResultSetToMovie(rs));
            }
            return movies;
        } catch (SQLException e) {
            throw new RuntimeException("查询所有电影失败", e);
        }
    }
    
    @Override
    public void updateMovie(Movie movie) {
        String sql = "UPDATE movies SET name = ?, duration = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?";
        try {
            int rows = dbManager.executeUpdate(sql, movie.getName(), movie.getDuration(), movie.getId());
            if (rows == 0) {
                throw new RuntimeException("电影不存在: " + movie.getId());
            }
        } catch (SQLException e) {
            throw new RuntimeException("更新电影失败", e);
        }
    }
    
    @Override
    public void deleteMovie(String id) {
        String sql = "DELETE FROM movies WHERE id = ?";
        try {
            int rows = dbManager.executeUpdate(sql, id);
            if (rows == 0) {
                throw new RuntimeException("电影不存在: " + id);
            }
        } catch (SQLException e) {
            throw new RuntimeException("删除电影失败", e);
        }
    }
    
    @Override
    public boolean movieExists(String id) {
        String sql = "SELECT COUNT(*) FROM movies WHERE id = ?";
        try (ResultSet rs = dbManager.executeQuery(sql, id)) {
            if (rs.next()) {
                return rs.getInt(1) > 0;
            }
            return false;
        } catch (SQLException e) {
            throw new RuntimeException("检查电影是否存在失败", e);
        }
    }
    
    private Movie mapResultSetToMovie(ResultSet rs) throws SQLException {
        String id = rs.getString("id");
        String name = rs.getString("name");
        int duration = rs.getInt("duration");
        
        return new Movie(id, name, duration);
    }
} 